package com.fansz.FanszAPI.service.impl;

import com.fansz.FanszAPI.domain.Topic;
import com.fansz.FanszAPI.service.TopicService;
import com.fansz.FanszAPI.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void saveTopic(Topic topic) {
        // TODO Auto-generated method stub
        mongoTemplate.save(topic, "topic");
    }

    @Override
    public Topic findByCreateTime(String createTime) {
        // TODO Auto-generated method stub
        if(!StringUtils.isEmpty(createTime))
        {
            return mongoTemplate.findOne(new Query(Criteria.where("createTime").is(createTime)), Topic.class, "topic");
        }
        else
        {
            return null;
        }

    }

    @Override
    public List<Topic> findByUser(String userId, int count, String startTime) {
        // TODO Auto-generated method stub
        if(!StringUtils.isEmpty(userId))
        {
            Query query = new Query(Criteria.where("userId").is(userId).and("createTime").gt(startTime));
            query.limit(count);
            return mongoTemplate.find(query, Topic.class, "topic");
        }
        else
        {
            return null;
        }

    }
    
}
